'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = '这是blog的数据页面';
  }

  // 导航栏的按钮列表
  async getTypeList() {
    const res = await this.app.mysql.select('type');
    this.ctx.body = res;
  }

  // 获取首页的列表:总列表+推荐列表+技术类别
  async getHomeArticle() {
    const sql = 'SELECT a.id, a.title, a.type, a.synopsis, a.img, a.skillTypeId, a.isRecommend, s.name as skillType ' +
      'FROM article as a ' +
      'left outer join skillType as s ' +
      'on a.skillTypeId = s.id ' +
      'WHERE a.isRecommend="1" ' +
      'ORDER BY a.updateTime desc';
    const sqlAll = 'SELECT article.id, article.title, article.type, article.synopsis, article.img, article.isRecommend, article.people, article.skillTypeId, article.updateTime, skillType.name as skillType ' +
      'FROM article ' +
      'left outer join skillType ' +
      'on article.skillTypeId = skillType.id ' +
      'ORDER BY updateTime desc';
    const sqlSkillType = 'SELECT skillType.id as id ,' +
      'skillType.name as name ' +
      'FROM skillType ';
    const isRecommendRes = this.app.mysql.query(sql);
    const allRes = this.app.mysql.query(sqlAll);
    const skillType = this.app.mysql.query(sqlSkillType);
    const res = Promise.all([ isRecommendRes, allRes, skillType ]).then(res => {
      return [{ isRecommendRes: res[0] }, { allRes: res[1] }, { skillType: res[2] }];
    });
    this.ctx.body = await res;
    // this.ctx.body = [{ isRecommendRes }, { allRes }, { skillType }];
  }

  // 对应技术类别的列表
  async getSkillTypeList() {
    const id = this.ctx.params.id;
    const sql = 'SELECT article.id as id ,' +
      'article.title as title ,' +
      'article.type as type ,' +
      'article.synopsis as synopsis ,' +
      'article.img as img ,' +
      'article.isRecommend as isRecommend ,' +
      'article.people as people ,' +
      'article.skillTypeId as skillTypeId ,' +
      'article.updateTime as updateTime ' +
      'FROM article ' +
      'WHERE skillTypeId=' + id;
    const res = await this.app.mysql.query(sql);
    this.ctx.body = res;
  }

  // 关键词列表
  async getKeywordList() {
    const str = this.ctx.params.str;
    const sql = 'SELECT article.id as id ,' +
      'article.title as title ,' +
      'article.type as type ,' +
      'article.synopsis as synopsis ,' +
      'article.img as img ,' +
      'article.isRecommend as isRecommend ,' +
      'article.people as people ,' +
      'article.skillTypeId as skillTypeId ,' +
      'article.updateTime as updateTime ' +
      'FROM article ' +
      'WHERE title like "%' + str + '%" || synopsis like "%' + str + '%" || content like "%' + str + '%"';
    const res = await this.app.mysql.query(sql);
    this.ctx.body = res;
  }

  // 获取详情页
  async getArticleDetails() {
    const id = this.ctx.params.id;
    const sql = 'SELECT a.id, a.title, a.type, a.synopsis, a.img, a.isRecommend, a.people, a.skillTypeId, a.content, a.tags, a.alterTime, a.updateTime, s.name as skillType ' +
      'FROM article as a ' +
      'left outer join skillType as s ' +
      'on a.skillTypeId = s.id ' +
      'WHERE a.id=' + id;
    const res = await this.app.mysql.query(sql);

    res[0].people = res[0].people - 0 + 1;
    // yzyconsole.log('res', res[0]);
    // 向数据库插入数据 阅读人数+1
    await this.app.mysql.update('article', { id, people: res[0].people });

    this.ctx.body = res;
  }

  // 获取生活
  async getLiveList() {
    const res = await this.app.mysql.select('livelist');
    this.ctx.body = res;
  }

  // 获取我的简介
  async getInteract() {
    const mydetailsRes = this.app.mysql.select('mydetails');
    // 留言
    const boardSql = 'select * from board order by updateTime desc';
    const boardRes = this.app.mysql.query(boardSql);
    const res = Promise.all([ mydetailsRes, boardRes ]).then(res => {
      return [{ mydetailsRes: res[0] }, { boardRes: res[1] }];
    });
    this.ctx.body = await res;
  }

  // 句子
  async getSentence() {
    const id = Math.floor(Math.random() * (13 - 1 + 1) + 1);
    const sql = 'SELECT sentence.id as id ,' +
      'sentence.content as content ' +
      'FROM sentence ' +
      'WHERE id=' + id;
    const res = await this.app.mysql.query(sql);
    this.ctx.body = res;
  }

  //   获取技术类型
  async getSkillType() {
    const res = await this.app.mysql.select('skillType');
    this.ctx.body = res;
  }

  //   新增留言
  async sendMessage() {
    // eslint-disable-next-line no-unused-vars
    const { replyName, boardId, ...info } = this.ctx.request.body;
    // yzyconsole.log('info', info);

    // 向数据库插入数据
    const result = await this.app.mysql.insert('board', info);
    // 判断是否成功,插入成功返回的row数
    const insertSuccess = result.affectedRows === 1;
    // 保存返回修改的id
    const insertId = result.insertId;

    this.ctx.body = {
      isScuccess: insertSuccess,
      insertId,
    };
  }

  //   新增留言回复
  async replyMessage() {
    // eslint-disable-next-line no-unused-vars
    const { avatar, ...info } = this.ctx.request.body;
    // yzyconsole.log('info', info);

    // 向数据库插入数据
    const result = await this.app.mysql.insert('replyboard', info);
    // 判断是否成功,插入成功返回的row数
    const insertSuccess = result.affectedRows === 1;
    // 保存返回修改的id
    const insertId = result.insertId;

    this.ctx.body = {
      isScuccess: insertSuccess,
      insertId,
    };
  }
}

module.exports = HomeController;
